<?php
 if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Manager extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->checkLevel1();
        $this->data['controller'] = 'manager';   
        $this->load->model('Mmanager');
    }
    
	public function listUser()
    {
        $this->load->library('pagination');
        $config['per_page'] = $this->data['_per_page'] = 4;
        $this->data['page'] = $this->data['page_current'] = $this->getParamUri(2,1);
        $offset = (($this->getParamUri(2,1) - 1) * $config['per_page']) > 0 ? (($this->getParamUri(2,1) - 1) * $config['per_page']) : 0;
        $data = $this->Mmanager->getListUser($config['per_page'],$offset);
        $config['base_url'] = base_url($this->uri->segment(1).'/');
        $config['total_rows'] = $data['count'];
        $config['uri_segment'] = 2;
        $config['uri_segment_page'] = $this->getParamUri(2,1);
        $config['suffix'] = '.html';
        $config['use_page_numbers'] = TRUE;
        $this->pagination->initialize($config);
        $this->data['data'] = $data['data'];
        
        $this->data['action'] = __FUNCTION__;
        $this->load->view('template/layout', $this->data);
    }
    
    public function add()
    {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('username', 'Tên đăng nhập', 'required');
        $this->form_validation->set_rules('password', 'Mật khẩu', 'required');
        $this->form_validation->set_rules('fullname', 'Tên đầy đủ', 'required');
        $this->form_validation->set_rules('dateofbirth', 'Ngày sinh', 'required');
        $this->form_validation->set_rules('display_name', 'Trên hiển thị', 'required');
        $this->form_validation->set_rules('email', 'Email', 'required');
        $this->form_validation->set_rules('mobile', 'Số điện thoại', 'required');
        
        if($this->form_validation->run() === TRUE)
        {
            $data = array(
                'username' => $this->getParamString('username'),
                'pword' => md5(md5($this->getParamString('username')).md5($this->getParamString('password'))),
                'fullname' => $this->getParamString('fullname'),
                'display_name' => $this->getParamString('display_name'),
                'dateofbirth' => date('Y-m-d', strtotime($this->getParamString('dateofbirth'))),
                'email' => $this->getParamString('email'),
                'mobile' => $this->getParamString('mobile'),
                'active' => 1,
                'level' => $this->getParamInt('level'),
                'created' => date('Y-m-d H:i:s')
            );
            
            $f = $this->Mmanager->addUser($data);
            if($f == '200'){
                $this->Mmanager->sendMail($data);
                die('<meta charset="utf-8"><script >alert("Thêm tài khoản thành công!");window.location="'.$this->data['baseurl'].'manager-user";</script>');
            }else{
                die('<meta charset="utf-8"><script >alert("Tên đăng nhập đã tồn tại!");window.location="'.$this->data['baseurl'].'manager-user";</script>');
            }
        }
        
        $this->data['action'] = __FUNCTION__;
        $this->load->view('template/layout', $this->data);
    }
    
    public function edit()
    {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('fullname', 'Tên đầy đủ', 'required');
        $this->form_validation->set_rules('dateofbirth', 'Ngày sinh', 'required');
        $this->form_validation->set_rules('display_name', 'Trên hiển thị', 'required');
        $this->form_validation->set_rules('email', 'Email', 'required');
        $this->form_validation->set_rules('mobile', 'Số điện thoại', 'required');
        
        $id = $this->getParamUri(2,1);
        $this->data['data'] = $this->Mmanager->getUserInfo($id);
        
        if($this->form_validation->run() === TRUE)
        {
            $data = array(
                'fullname' => $this->getParamString('fullname'),
                'display_name' => $this->getParamString('display_name'),
                'dateofbirth' => date('Y-m-d', strtotime($this->getParamString('dateofbirth'))),
                'email' => $this->getParamString('email'),
                'mobile' => $this->getParamString('mobile'),
                'active' => 1,
                'level' => $this->getParamInt('level')
            );
            
            if(!in_array($this->getParamString('password'), array(null, ''))){
                $data['pword'] =  md5(md5($this->getParamString('username')).md5($this->getParamString('password')));
            }
            
            $f = $this->Mmanager->editUser($id, $data);
            if($f == '200'){
                die('<meta charset="utf-8"><script >alert("Cập nhật thông tin tài khoản thành công!");window.location="'.$this->data['baseurl'].'manager-user";</script>');
            }
        }
        
        $this->data['action'] = __FUNCTION__;
        $this->load->view('template/layout', $this->data);
    }
    
    public function delete()
    {
        $f = $this->Mmanager->deleteUser($this->getParamUri(2,1));
        if($f == '200'){
            redirect('manager-user');
        }
        
        $this->data['action'] = __FUNCTION__;
        $this->load->view('template/layout', $this->data);
    }
}
